package com.mgy.shardingjdbc.test.web.controller.notepad;


import com.mgy.adapter.factory.AdapterFactory;
import com.mgy.db.TableRouter;
import com.mgy.shardingjdbc.test.dao.mapper.notepad.INotepadDao;
import com.mgy.shardingjdbc.test.domain.notepad.Notepad;
import com.mgy.shardingjdbc.test.domain.notepad.Tag;
import com.mgy.shardingjdbc.test.domain.notepad.TagAndNotepad;
import com.mgy.shardingjdbc.test.service.notepad.NotepadService;
import com.mgy.shardingjdbc.test.service.person.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("/notepad")
@RestController
public class NotepadController {
    @Autowired
    private NotepadService notepadService;

    @Autowired
    private INotepadDao notepadDao;

    @RequestMapping("/add0")
    public Object add0() {
        Notepad notepad = new Notepad();
        notepad.setUserId(123456);
        notepad.setContent("测试内容");
        notepad.setTitle("标题1");
        notepad.setTagId(1L);
        notepad.setCreated(new Date());
        notepad.setModified(new Date());
        TableRouter router = new TableRouter();
        router.setDbNo(2);
        router.setTableNo(0);

        Integer id = notepadDao.add(notepad,router);
        System.out.println("id：" + id);
        System.out.println(notepad.toString());
        return id;
    }

    @RequestMapping("/add")
    public Object add(Notepad notepad) {
        notepadService.add(notepad);
        return "添加成功";
    }

    @RequestMapping("/getByKey")
    public Object getByKey(Integer id) {
        return notepadService.getOne(id);
    }


    @RequestMapping("/getList2")
    public Object getList2() {
        List<TagAndNotepad> list = notepadService.getTagAndNotepad(1L);
        return list;
    }

    @RequestMapping("/addNotepadAndTag")
    public Object addNotepadAndTag() {
        Notepad notepad = new Notepad();
        notepad.setUserId(111);
        notepad.setContent("shiwu_notepad");
        notepad.setCreated(new Date());
        notepad.setModified(new Date());
        notepad.setTitle("notepad_biaoti");

        Tag tag = new Tag();
        tag.setName("shiwu_tag1");
        tag.setEnableStatus(1);
        tag.setCreated(new Date());
        tag.setModified(new Date());
        notepadService.addNotepadAndTag(notepad, tag);
        return "添加成功";
    }

    @RequestMapping("/getPerson")
    public Object getPerson() {
        Person p1 = AdapterFactory.getInstance(Person.class, "p1");
        p1.say();

        Person p2 = AdapterFactory.getInstance(Person.class, "p2");
        p1.say();

        Person p3 = AdapterFactory.getInstance(Person.class, "p3");
        p1.say();

        Person p4 = AdapterFactory.getInstance(Person.class, "p4");
        p1.say();

        p1 = AdapterFactory.getInstance(Person.class, "p1");
        p1.say();
        return p1;
    }

    @RequestMapping("/getOne2")
    public Object getOne2(Integer id) {
        return notepadService.getOne2(id);
    }
}
